Peter Langfelder and Steve Horvath tutorials are here

Paper: Navarro et al, 2021

createDT <- function(DF, caption="", scrollY=500){
  data <- DT::datatable(DF, caption=caption,
    extensions =  'Buttons',
    options = list( dom = 'Bfrtip', 
                    buttons = c('copy', 'csv', 'excel', 'pdf', 'print'), 
                    scrollY = scrollY, scrollX=T, scrollCollapse = T, paging = F,  
                      columnDefs = list(list(className = 'dt-center', targets = "_all"))
    )
  ) 
   return(data)
}
# if (!requireNamespace("BiocManager", quietly = TRUE))
#     install.packages("BiocManager")

# if(!require("limma")) BiocManager::install("limma"); library("limma")
library(edgeR)
library(sva)
library(limma)
library(factoextra)
library(WGCNA)
library(data.table)
library(flashClust)
library(DESeq2)
library(tidyverse)

Download dataset

# https://zenodo.org/record/4715907
expr_dir = "/pastel/Github_scripts/Intro_Systems_Biology/datasets/"

# Metadata 
metadata = as.data.frame(data.table::fread(paste0(expr_dir, "MyND/MyND_metadata.txt"), header = T, stringsAsFactors = F, check.names = F))

# Counts expression data 
data = data.table::fread(paste0(expr_dir, "MyND/monocyte_counts_matrix.txt"), header = T, stringsAsFactors = F, check.names = F)
# dim(data)
# data[1:5, 1:5]
data_tmp = as.data.frame(data)
rownames(data_tmp) = data$ENSEMBL_ID
data_tmp$ENSEMBL_ID = NULL
data_tmp = data_tmp[, metadata$rnaseq_id] # order the expression table 
identical(colnames(data_tmp), metadata$rnaseq_id) # must be TRUE 
## [1] TRUE
data_tmp[1:3,1:3]
##                    ADRC-111-01_1 ADRC-11955-01_2 ADRC-18133-01_1
## ENSG00000000003.14             0               1               0
## ENSG00000000005.6              0               0               0
## ENSG00000000419.12           295            1246             426
x <- DGEList(counts=as.matrix(data_tmp), samples=metadata)
cpm = cpm(x)
# cpm[1:3,1:3]

keep.exp = rowSums(cpm > 1) >= 0.3*ncol(x) # Filter non-expressed genes
x = x[keep.exp,]
dim(x$counts)
## [1] 13667   230
counts_filt = as.data.frame(x$counts) # counts filtered matrix 
# counts_filt[1:3, 1:3]

PCA before adjustment

# Let's factor be factor! 
metadata$sex = as.factor(metadata$sex)
metadata$diagnosis = as.factor(metadata$diagnosis)
metadata$aj_status = as.factor(metadata$aj_status)
metadata$population_assignment = as.factor(metadata$population_assignment)

# Data normalization 
dds <- DESeqDataSetFromMatrix(countData = round(counts_filt),
                             colData = metadata,
                             design = ~ diagnosis)
## converting counts to integer mode
gExpr <- DGEList(counts=assay(dds))
gExpr <- calcNormFactors(gExpr)
vobjGenes <- voom(gExpr, model.matrix( ~ diagnosis, metadata) )
gene_counts_voom = vobjGenes$E # normalized matrix 

res.pca = prcomp(t(gene_counts_voom))
fviz_pca_ind(res.pca, 
             habillage = metadata$diagnosis)

PCA after adjustment

# SVA network for data adjustment 
mod0 <- model.matrix(~ 1, colData(dds))
mod <- model.matrix(design(dds), colData(dds))
nsv <- num.sv(gene_counts_voom, mod, method = "be")
message(paste0("Number of SVs proposed: ", nsv)) 
## Number of SVs proposed: 12
resid_expr = sva_network(gene_counts_voom, nsv) # matrix adjusted
resid_expr_t = t(resid_expr)

res.pca2 = prcomp(resid_expr_t)
fviz_pca_ind(res.pca2,
             habillage = metadata$diagnosis)

Power threshold

# Choose a set of soft-thresholding powers
powers = c(c(1:10), seq(from = 12, to=20, by=2))

# Call the network topology analysis function
sft = pickSoftThreshold(resid_expr_t, powerVector = powers, verbose = 5)
## pickSoftThreshold: will use block size 3273.
##  pickSoftThreshold: calculating connectivity for given powers...
##    ..working on genes 1 through 3273 of 13667
## Warning: executing %dopar% sequentially: no parallel backend registered
##    ..working on genes 3274 through 6546 of 13667
##    ..working on genes 6547 through 9819 of 13667
##    ..working on genes 9820 through 13092 of 13667
##    ..working on genes 13093 through 13667 of 13667
##    Power SFT.R.sq slope truncated.R.sq  mean.k. median.k.  max.k.
## 1      1    0.198 -2.15          0.991 1.24e+03  1.23e+03 2110.00
## 2      2    0.590 -2.52          0.998 1.87e+02  1.77e+02  534.00
## 3      3    0.796 -2.64          0.997 3.78e+01  3.27e+01  179.00
## 4      4    0.885 -2.59          0.997 9.53e+00  7.23e+00   70.90
## 5      5    0.937 -2.40          0.992 2.89e+00  1.82e+00   33.70
## 6      6    0.973 -2.26          0.989 1.03e+00  5.19e-01   21.70
## 7      7    0.979 -2.04          0.978 4.28e-01  1.61e-01   16.40
## 8      8    0.949 -1.88          0.941 2.06e-01  5.38e-02   13.50
## 9      9    0.964 -1.69          0.956 1.13e-01  1.92e-02   11.30
## 10    10    0.956 -1.55          0.945 7.06e-02  7.07e-03    9.69
## 11    12    0.960 -1.32          0.953 3.64e-02  1.08e-03    7.32
## 12    14    0.934 -1.26          0.915 2.43e-02  1.85e-04    6.99
## 13    16    0.386 -1.82          0.339 1.87e-02  3.33e-05    6.98
## 14    18    0.382 -1.97          0.336 1.57e-02  6.50e-06    6.98
## 15    20    0.386 -1.93          0.337 1.39e-02  1.30e-06    6.98
# Scale-free topology fit index as a function of the soft-thresholding power
cex1 = 0.9

plot(sft$fitIndices[,1], -sign(sft$fitIndices[,3])*sft$fitIndices[,2],
xlab="Soft Threshold (power)",ylab="Scale Free Topology Model Fit,signed R^2",type="n",
main = paste("Scale independence"))
text(sft$fitIndices[,1], -sign(sft$fitIndices[,3])*sft$fitIndices[,2],
labels=powers,cex=cex1,col="red")
# this line corresponds to using an R^2 cut-off of h
abline(h=0.90,col="red")

Connectivity

# Mean connectivity as a function of the soft-thresholding power
plot(sft$fitIndices[,1], sft$fitIndices[,5],
xlab="Soft Threshold (power)",ylab="Mean Connectivity", type="n",
main = paste("Mean connectivity"))
text(sft$fitIndices[,1], sft$fitIndices[,5], labels=powers, cex=cex1,col="red")

TOM adjacency

softPower = 5
adjacency = adjacency(resid_expr_t, power = softPower, type = "signed") 
TOM = TOMsimilarity(adjacency, TOMType="signed")
## ..connectivity..
## ..matrix multiplication (system BLAS)..
## ..normalization..
## ..done.
dissTOM = 1-TOM
geneTree = flashClust(as.dist(dissTOM), method="average")

plot(geneTree, xlab="", sub="", main= "Gene Clustering on TOM-based dissimilarity", labels= FALSE, hang=0.04)

Net parameters

# "We like large modules" - Tutorial, so we set the minimum module size relatively high:
minModuleSize = 30
# Module identification using dynamic tree cut:
dynamicMods = cutreeDynamic(dendro = geneTree, 
                            distM = dissTOM,
                            deepSplit = 3, 
                            pamRespectsDendro = FALSE,
                            minClusterSize = minModuleSize);

..cutHeight not given, setting it to 0.964 ===> 99% of the (truncated) height range in dendro. ..done.

clusters_size = data.matrix(table(dynamicMods))
clusters_size = cbind(rownames(clusters_size),clusters_size)
colnames(clusters_size) = c("cluster","size")

# Convert numeric lables into colors
dynamicColors = labels2colors(dynamicMods)
clusters_colors = data.matrix(table(dynamicColors))
clusters_colors = cbind(rownames(clusters_colors), clusters_colors)
colnames(clusters_colors) = c("color","size")

mytable = table(dynamicColors)
# as.data.frame(t(as.matrix(unclass(mytable))))

Dendrogram colors

# Plot the dendrogram and colors underneath
plotDendroAndColors(geneTree, dynamicColors, "Dynamic Tree Cut",
dendroLabels = FALSE, hang = 0.03,
addGuide = TRUE, guideHang = 0.05,
main = "Gene dendrogram and module colors")

Number of modules before merge

length(unique(dynamicColors))

[1] 88

Clustering eigengenes

# Calculate eigengenes
MEList = moduleEigengenes(resid_expr_t, colors = dynamicColors)
MEs = MEList$eigengenes
# Calculate dissimilarity of module eigengenes
MEDiss = 1-cor(MEs)
# Cluster module eigengenes
METree = hclust(as.dist(MEDiss), method = "average")

plot(METree, main = "Clustering of module eigengenes",
xlab = "", sub = "")
#We choose a height cut of 0.25, corresponding to correlation of 0.75, to merge
MEDissThres = 0.25
# Plot the cut line into the dendrogram
abline(h=MEDissThres, col = "red")

Merge modules

# Call an automatic merging function
merge = mergeCloseModules(resid_expr_t, dynamicColors, cutHeight = MEDissThres, verbose = 3)
##  mergeCloseModules: Merging modules whose distance is less than 0.25
##    multiSetMEs: Calculating module MEs.
##      Working on set 1 ...
##      moduleEigengenes: Calculating 88 module eigengenes in given set.
##    multiSetMEs: Calculating module MEs.
##      Working on set 1 ...
##      moduleEigengenes: Calculating 87 module eigengenes in given set.
##    Calculating new MEs...
##    multiSetMEs: Calculating module MEs.
##      Working on set 1 ...
##      moduleEigengenes: Calculating 87 module eigengenes in given set.
# The merged module colors
mergedColors = merge$colors;
# Eigengenes of the new merged modules:
mergedMEs = merge$newMEs
#png(paste0(expr_dir, "/MyND/Merge_modules.png"), width = 16, height = 8, res = 300, units = "in")
plotDendroAndColors(geneTree, cbind(dynamicColors, mergedColors),
c("Dynamic Tree Cut", "Merged dynamic"),
dendroLabels = FALSE, hang = 0.03,
addGuide = TRUE, guideHang = 0.05)

Merged network

#### For use of the new MERGED DATA!!! 
# Rename to moduleColors
moduleColors = mergedColors
# Construct numerical labels corresponding to the colors
colorOrder = c("grey", standardColors(50));
moduleLabels = match(moduleColors, colorOrder)-1;
MEs = mergedMEs;
# Save module colors and labels for use in subsequent parts
save(MEs, mergedMEs, moduleColors, geneTree, adjacency, file = paste0(expr_dir, "MyND/WGCNA_monocytes.RData"))

Number of modules after merge

length(unique(moduleColors))

[1] 87

Trait associations

metadata_selected2 = metadata[,c("diagnosis", "sex", "age")]
metadata_selected2$diagnosis = as.numeric(metadata_selected2$diagnosis)
metadata_selected2$sex = as.numeric(metadata_selected2$sex)
metadata_selected2$age = as.numeric(metadata_selected2$age)
nGenes = ncol(resid_expr_t)
nSamples = nrow(resid_expr_t)

# Recalculate MEs with color labels
MEs0 = moduleEigengenes(resid_expr_t, moduleColors)$eigengenes
MEs = orderMEs(MEs0) #Reorder eigenvectors such that similar ones measured by correlation are next to each other
moduleTraitCor = WGCNA::cor(MEs, metadata_selected2, use = "p")
moduleTraitPvalue = corPvalueStudent(moduleTraitCor, nSamples)

# Will display correlations and their p-values
textMatrix = paste(signif(moduleTraitCor, 2), "\n(",
signif(moduleTraitPvalue, 1), ")", sep = "")
dim(textMatrix) = dim(moduleTraitCor)
par(mar = c(6, 20, 3, 3));
# Display the correlation values within a heatmap plot
labeledHeatmap(Matrix = moduleTraitCor,
xLabels = names(metadata_selected2), 
yLabels = names(MEs),
ySymbols = names(MEs),
colorLabels = FALSE,
colors = greenWhiteRed(50),
textMatrix = textMatrix,
setStdMargins = FALSE,
cex.text = 0.5,
zlim = c(-1,1),
main = paste("Module-trait relationships"))
## Warning in greenWhiteRed(50): WGCNA::greenWhiteRed: this palette is not suitable for people
## with green-red color blindness (the most common kind of color blindness).
## Consider using the function blueWhiteRed instead.

gene_ids = colnames(resid_expr_t)
gene_ids = colnames(resid_expr_t)
# save(moduleColors, file = paste0(work_plots, "MyND/moduleColors.Rdata"))
gene_modules = cbind(gene_ids, moduleColors)
gene_modules = as.data.frame(gene_modules)

Module assignment

## Get conversion table for Gencode 30
gencode_30 = read.table(paste0(expr_dir, "ens.geneid.gencode.v30"))
colnames(gencode_30) = c("ensembl","symbol")
gene_modules_symbol = merge(gene_modules, gencode_30, by.x = "gene_ids", by.y = "ensembl")

write.table(gene_modules_symbol, file = paste0(expr_dir, "MyND/geneBymodule.txt"), quote = F, row.names = F, sep = "\t")
createDT(gene_modules_symbol)
## Warning in instance$preRenderHook(instance): It seems your data is too big
## for client-side DataTables. You may consider server-side processing: https://
## rstudio.github.io/DT/server.html

Module size

mytable2 = table(gene_modules_symbol$moduleColors)
createDT(as.data.frame(t(as.matrix(unclass(mytable2)))))